package com.chuan.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

//Thread实现多线程下载图片
public class TestThread2 extends Thread{
    private String url;
    private String name;
    public TestThread2(String url, String name){
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        WebDownloader webDownloader = new WebDownloader();
        webDownloader.downloader(url, name);
        System.out.println("文件名："+name);
    }

    public static void main(String[] args) {
        TestThread2 t1 = new TestThread2("https://img2.baidu.com/it/u=3150567454,1089677718&fm=253&fmt=auto&app=138&f=PNG?w=600&h=369","1.jpg");
        TestThread2 t2 = new TestThread2("https://img1.baidu.com/it/u=470255208,2889809727&fm=253&fmt=auto&app=120&f=JPEG?w=950&h=443","2.jpg");
        TestThread2 t3 = new TestThread2("https://img1.baidu.com/it/u=1503222636,2429859130&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500","3.jpg");
        t1.start();
        t2.start();
        t3.start();
    }
}

//下载器
class WebDownloader{
    public void downloader(String url, String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("IO downloader方法异常");
        }
    }
}